package m.jclecai.com.fragment.refreshableList;

import android.view.View;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import m.jclecai.com.R;
import m.jclecai.com.adapter.base.DefaultViewHolderAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.listener.Callback;

/**
 * Created by Dinosa on 2017/11/21.
 * <p>
 * 一个上拉刷新和下拉加载的列表的一个Fragment的一个基类
 *
 *  这里继承的话，会比组合要好的多，这样就不需要将参数传来传去的；
 *
 *  这里我们使用mvp的方式对其进行要给优化；
 */

public abstract class RefreshableListFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2
{

    public int mPage = 1;

    protected PullToRefreshListView mPrListview;
    protected DefaultViewHolderAdapter mAdapter;
    private View mLlNoData;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_refreshalbe_list;
    }

    @Override
    protected void initLoadingView(View vBody) {
        super.initLoadingView(vBody);
        mPrListview = ((PullToRefreshListView) vBody.findViewById(R.id.pr_listview));

        mLlNoData = vBody.findViewById(R.id.ll_no_data);
        mPrListview.setEmptyView(mLlNoData);
    }

    @Override
    public void initView() {
        super.initView();

        mPrListview.setMode(PullToRefreshBase.Mode.BOTH);
        mPrListview.setOnRefreshListener(this);
        mAdapter = createAdapter();
        mPrListview.setAdapter(mAdapter);
    }



    @Override
    public void initData() {
        super.initData();
        //这里我们判断是否需要登陆；
        if (isNeedLogin()) {

            checkLogin(new Callback(){
                @Override
                public void callbackField() {
                    super.callbackField();
                    DisPlay("请先登陆!!!");
                }

                @Override
                public void callbackSuccess() {
                    super.callbackSuccess();
                    requestData();
                }
            });
        }else{
            requestData();
        }

    }



    public boolean isNeedLogin() {
        return false;
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        mPage=1;
        mAdapter.clear();
        requestData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        requestData();
    }


    public  abstract void requestData();
    protected abstract DefaultViewHolderAdapter createAdapter();
}
